package com.jrx.anytxn.transaction.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.TlLoanClassifyChange;
import com.jrx.anytxn.transaction.service.ITlLoanClassifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 五级分类变动处理类
 */
@Service
public class TlLoanClassifyServiceImpl implements ITlLoanClassifyService {

    private static final Logger logger = LoggerFactory.getLogger(TlLoanClassifyServiceImpl.class);


    @Autowired
    private ISegmentService segmentService;

    @Override
    public TlLoanClassifyChange buildLoanClassifyChange(AmLoanAccountInfo amLoanAccountInfo, Date businessDate, BigDecimal postAmt, String afterClass) throws TxnBizException {
        //这个方法只是放款成功，结清后调用
        TlLoanClassifyChange tlLoanClassifyChange = new TlLoanClassifyChange();
        try {
            tlLoanClassifyChange.setId(segmentService.getId(TransBizConstant.TL_LOAN_CLASSIFY_CHANGE_ID));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlLoanClassifyChange.setTenantId(amLoanAccountInfo.getTenantId());
        tlLoanClassifyChange.setOrganizationId(amLoanAccountInfo.getOrganizationId());
        tlLoanClassifyChange.setCustomerId(amLoanAccountInfo.getCustomerId());
        tlLoanClassifyChange.setLoanId(amLoanAccountInfo.getLoanId());
        tlLoanClassifyChange.setChannel(amLoanAccountInfo.getChannel());
        tlLoanClassifyChange.setBusinessDate(DateUtils.format(businessDate,DateUtils.DATE_PATTERN_ANOTHER));
        tlLoanClassifyChange.setDelqDays(0);
        tlLoanClassifyChange.setBeforeStatus(amLoanAccountInfo.getLoanClassify());
        tlLoanClassifyChange.setAfterStatus(afterClass);
        tlLoanClassifyChange.setPrincipalAmount(postAmt);

        return tlLoanClassifyChange;
    }

    @Override
    public TlLoanClassifyChange buildClassifyChange(Date businessDate, AmLoanAccountInfo amLoanAccountInfo,int delqMaxDays,
                                                    String beforeStatus,String afterStatus) throws TxnBizException {
        TlLoanClassifyChange tlLoanClassifyChange = new TlLoanClassifyChange();
        try {
            tlLoanClassifyChange.setId(segmentService.getId(TransBizConstant.TL_LOAN_CLASSIFY_CHANGE_ID));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlLoanClassifyChange.setTenantId(amLoanAccountInfo.getTenantId());
        tlLoanClassifyChange.setOrganizationId(amLoanAccountInfo.getOrganizationId());
        tlLoanClassifyChange.setCustomerId(amLoanAccountInfo.getCustomerId());
        tlLoanClassifyChange.setLoanId(amLoanAccountInfo.getLoanId());
        tlLoanClassifyChange.setChannel(amLoanAccountInfo.getChannel());
        tlLoanClassifyChange.setBusinessDate(DateUtils.format(businessDate,DateUtils.DATE_PATTERN_ANOTHER));
        tlLoanClassifyChange.setDelqDays(delqMaxDays);
        tlLoanClassifyChange.setBeforeStatus(beforeStatus);
        tlLoanClassifyChange.setAfterStatus(afterStatus);
        tlLoanClassifyChange.setPrincipalAmount(amLoanAccountInfo.getRemainPrincipal());

        return tlLoanClassifyChange;
    }
}
